<?php
/**
 * Created by PhpStorm.
 * User: niqiaoyun
 * Date: 2017/9/6
 * Time: 15:32
 */

namespace app\models;


use app\models\dao\Project;

class ProjectModel extends Project {

	// 有效状态
	const STATUS_VALID = 1;

	const FLAG_VALID = 0;

	// 测试环境
	const ENV_DEV  = 1;

	// 仿真环境
	const ENV_PROD  = 2;

	// 线上环境
	const ENV_ONLINE  = 3;

	const REPO_TYPE_GIT = 'git';

	const REPO_TYPE_SVN = 'svn';

	const REPO_MODE_BRANCH = 'branch';

	const REPO_MODE_TAG = 'tag';

	const REPO_MODE_NOT_TRUNK = 'nontrunk';

	public static $CONFIG;

	public static $ENV = [
			self::ENV_DEV => 'dev',
			self::ENV_PROD => 'prod',
			self::ENV_ONLINE => 'online',
	];

	public function init() {
		parent::init(); // TODO: Change the autogenerated stub
	}

	/**
	 * 根据查询条件所有信息
	 * @param $ary_condition 查询条件
	 * @param string $str_select_fields 查询列名
	 * @param string $str_order 排序方式
	 * @return array|null|\yii\db\ActiveRecord
	 */
	public function searchByConditionAll($ary_condition, $page_no = 1, $page_size = 20, $orderBy = 'create_time desc') {
		//查询条件处理
		$str_condition = '`flag`=:flag';
		$array_params = [':flag' => 0];
		$andFilterWhere = [];


		if(isset($ary_condition['keyword']) && false == empty($ary_condition['keyword'])){
			$andFilterWhere = [
				"like",
				"name",
				$ary_condition['keyword']
			];
		}

		//分页
		$selectFiled = '*';
		$offset = ($page_no - 1) * $page_size;
		$limit = $page_size;

		$str_order = $orderBy;

		//获取所有记录
		$total_results = $this->find()->where($str_condition, $array_params)->andFilterWhere($andFilterWhere)->count();
		if ($total_results <= 0) {
			return ['total_results' => $total_results, 'datalist' => []];
		}

		$array_lists = $this->find()->select ( $selectFiled )->where($str_condition, $array_params)->andFilterWhere($andFilterWhere)->orderBy($str_order)->offset($offset)->limit($limit)->asArray()->all();
		return self::formatPage($array_lists, $total_results);
	}

	public function searchListByIds($ary_condition) {
		$ary_result = [];

		try {
			$ary_result = $this->find ()
					->select( '*' )
					->where ( $ary_condition )
					->asArray ()
					->all ();
		} catch (\Exception $e) {

		}
		return $ary_result;
	}

	/**
	 * 获取当前进程的项目配置
	 *
	 * @param $id
	 * @return string|\yii\db\ActiveQuery
	 */
	public static function getConfig($id = null) {
		if (empty(static::$CONFIG)) {
			static::$CONFIG = static::findOne(['id' => $id, 'flag' => 0]);
		}
		return static::$CONFIG;
	}

	/**
	 * 拼接宿主机的仓库目录
	 * {deploy_from}/{env}/{project}
	 *
	 * @return string
	 */
	public static function getDeployFromDir() {
		$from    = static::$CONFIG->deploy_from;
		$env     = isset(static::$ENV[static::$CONFIG->level]) ? static::$ENV[static::$CONFIG->level] : 'unknow';
		$project = static::getGitProjectName(static::$CONFIG->repo_url);

		return sprintf("%s/%s/%s", rtrim($from, '/'), rtrim($env, '/'), $project);
	}

	/**
	 * 拼接宿主机的部署隔离工作空间
	 * {deploy_from}/{env}/{project}-YYmmdd-HHiiss
	 *
	 * @return string
	 */
	public static function getDeployWorkspace($version) {
		$from    = static::$CONFIG->deploy_from;
		$env     = isset(static::$ENV[static::$CONFIG->level]) ? static::$ENV[static::$CONFIG->level] : 'unknow';
		$project = static::getGitProjectName(static::$CONFIG->repo_url);

		return sprintf("%s/%s/%s-%s", rtrim($from, '/'), rtrim($env, '/'), $project, $version);
	}

	/**
	 * 根据git地址获取项目名字
	 *
	 * @param $gitUrl
	 * @return mixed
	 */
	public static function getGitProjectName($gitUrl) {
		if (preg_match('#.*/(.*?)\.git#', $gitUrl, $match)) {
			return $match[1];
		}

		return basename($gitUrl);
	}

}